<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="72px"
        >
            <el-form-item label="应用" prop="appCode">
                <el-select
                    v-model="queryParams.appCode"
                    placeholder="请选择应用"
                    clearable
                    @keyup.enter.native="handleQuery"
                >
                    <el-option
                        v-for="item in appDict"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="角色" prop="roleId">
                <el-select
                    v-model="queryParams.roleId"
                    placeholder="请选择角色"
                    clearable
                    @keyup.enter.native="handleQuery"
                >
                    <el-option
                        v-for="item in roleDict"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="功能名称" prop="funcName">
                <el-input
                    v-model="queryParams.funcName"
                    placeholder="请输入功能名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="操作名称" prop="operationText">
                <el-input
                    v-model="queryParams.operationText"
                    placeholder="请输入操作名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="BUG状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请选择BUG状态"
                    clearable
                    @keyup.enter.native="handleQuery"
                >
                    <el-option
                        v-for="item in bugDict"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <div
                class="mb8"
                style="display: flex; justify-content: space-between"
            >
                <div>
                    <el-button
                        type="info"
                        plain
                        icon="el-icon-upload2"
                        size="mini"
                        @click="handleImport"
                        >导入</el-button
                    >
                    <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['system:defect:export']"
                        >导出</el-button
                    >
                </div>
                <div>
                    <el-button
                        type="primary"
                        plain
                        size="mini"
                        @click="allStartOrStop(0)"
                        >全开启</el-button
                    >
                    <el-button
                        type="danger"
                        plain
                        size="mini"
                        @click="allStartOrStop(1)"
                        >全关闭</el-button
                    >
                </div>
            </div>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="defectList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    label="序号"
                    type="index"
                    align="center"
                    width="50"
                />
                <el-table-column label="Bug编号" align="center" prop="code" />
                <el-table-column label="应用" align="center" prop="app" />
                <el-table-column label="角色" align="center" prop="roleName" />
                <el-table-column
                    label="功能名称"
                    align="center"
                    prop="funcName"
                />
                <el-table-column
                    label="操作名称"
                    align="center"
                    prop="operationText"
                />
                <el-table-column
                    label="文件名称"
                    align="center"
                    prop="fileName"
                />
                <el-table-column label="BUG点" align="center" prop="bugName" />
                <el-table-column
                    label="预期(步骤与结果)"
                    align="center"
                    prop="expect"
                />
                <el-table-column
                    label="实际(步骤与结果)"
                    align="center"
                    prop="actual"
                />

                <el-table-column label="BUG状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <el-switch
                            @change="handleStatusChange(scope.row)"
                            active-text="开"
                            inactive-text="关"
                            v-model="scope.row.status"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            active-value="0"
                            inactive-value="1"
                        >
                        </el-switch>
                    </template>
                </el-table-column>
                <!-- <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:defect:edit']"
                            >修改</el-button
                        >
                    </template>
                </el-table-column> -->
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改缺陷管理对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="1200px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="Bug编码" prop="code">
                                <el-input
                                    disabled
                                    v-model="form.code"
                                    placeholder="请输入Bug编码"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="应用" prop="appCode">
                                <el-select
                                    v-model="form.appCode"
                                    placeholder="请选择应用"
                                >
                                    <el-option
                                        v-for="item in appDict"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    >
                                    </el-option>
                                </el-select> </el-form-item
                        ></el-col>
                        <el-col :span="12"
                            ><el-form-item label="角色" prop="roleId">
                                <el-select v-model="form.roleId">
                                    <el-option
                                        v-for="item in roleDict"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="功能名称" prop="funcName">
                                <el-input
                                    v-model="form.funcName"
                                    placeholder="请输入功能名称"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="操作名称" prop="operationText">
                                <el-input
                                    v-model="form.operationText"
                                    placeholder="请输入操作名称"
                                /> </el-form-item
                        ></el-col>
                        <el-col :span="12">
                            <el-form-item label="文件名" prop="fileName">
                                <el-input
                                    v-model="form.fileName"
                                    placeholder="请输入文件名"
                                /> </el-form-item
                        ></el-col>

                        <el-col :span="12">
                            <el-form-item label="BUG点" prop="bugName">
                                <el-input
                                    v-model="form.bugName"
                                    placeholder="请输入BUG点"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="bug状态" prop="status">
                                <el-select v-model="form.status">
                                    <el-option
                                        v-for="item in bugDict"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="预期" prop="expect">
                                <el-input
                                    v-model="form.expect"
                                    type="textarea"
                                    :rows="5"
                                    placeholder="请输入内容"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="实际" prop="actual">
                                <el-input
                                    v-model="form.actual"
                                    type="textarea"
                                    :rows="5"
                                    placeholder="请输入内容"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12"> </el-col>
                    </el-row>
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 导入缺陷管理对话框 -->
        <!-- 用户导入对话框 -->
        <el-dialog
            :title="upload.title"
            :visible.sync="upload.open"
            width="400px"
            append-to-body
        >
            <el-upload
                ref="upload"
                :limit="1"
                accept=".xlsx, .xls"
                :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport"
                :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress"
                :on-success="handleFileSuccess"
                :auto-upload="false"
                drag
            >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                    将文件拖到此处，或<em>点击上传</em>
                </div>
                <div class="el-upload__tip text-center" slot="tip">
                    <div class="el-upload__tip" slot="tip">
                        <el-checkbox v-model="upload.updateSupport" />
                        是否更新已经存在的用户数据
                    </div>
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link
                        type="primary"
                        :underline="false"
                        style="font-size: 12px; vertical-align: baseline"
                        @click="importTemplate"
                    >
                        下载模板
                    </el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm"
                    >确 定</el-button
                >
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import {
    listDefect,
    getDefect,
    delDefect,
    addDefect,
    updateDefect,
    allOnOff,
} from "@/api/system/defect";

export default {
    name: "Defect",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 缺陷管理表格数据
            defectList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                isAsc: "descending",
                code: null,
                app: null,
                appCode: null,
                roleId: null,
                roleName: null,
                funcName: null,
                operationText: null,
                fileName: null,
                difficultyLevel: null,
                level: null,
                bugName: null,
                expect: null,
                actual: null,
                score: null,
                status: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                status: [
                    {
                        required: true,
                        message: "BUG状态不能为空",
                        trigger: "blur",
                    },
                ],
            },

            //缺陷导入参数
            upload: {
                // 是否显示弹出层
                open: false,
                // 弹出层标题
                title: "缺陷导入",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的缺陷数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/system/defect/importData",
            },
            appDict: [
                {
                    label: "web",
                    value: "0",
                },
                {
                    label: "app",
                    value: "1",
                },
            ],
            roleDict: [
                {
                    label: "系统管理员",
                    value: 112,
                },
                {
                    label: "人资管理员",
                    value: 113,
                },
                {
                    label: "普通职员",
                    value: 110,
                },
            ],
            bugDict: [
                {
                    label: "开",
                    value: "0",
                },
                {
                    label: "关",
                    value: "1",
                },
            ],
        };
    },
    created() {
        this.getList();
    },
    methods: {
        allStartOrStop(status) {
            this.$confirm(
                `确定要${status == 0 ? "开启" : "关闭"}所有BUG吗?`,
                "提示",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            ).then(() => {
                allOnOff({ status: status }).then((response) => {
                    this.$message({ type: "success", message: response.msg });
                    this.getList();
                });
            });
        },

        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "用户导入";
            this.upload.open = true;
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download(
                "system/user/importTemplate",
                {},
                `user_template_${new Date().getTime()}.xlsx`
            );
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert(
                "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
                    response.msg +
                    "</div>",
                "导入结果",
                { dangerouslyUseHTMLString: true }
            );
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
        /** 查询缺陷管理列表 */
        getList() {
            this.loading = true;
            listDefect(this.queryParams).then((response) => {
                this.defectList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                code: null,
                app: null,
                appCode: null,
                roleId: null,
                roleName: null,
                funcName: null,
                operationText: null,
                fileName: null,
                difficultyLevel: null,
                level: null,
                bugName: null,
                expect: null,
                actual: null,
                score: null,
                status: "0",
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加缺陷管理";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getDefect(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改缺陷管理";
            });
        },
        handleStatusChange(row) {
            let temp = { ...row };
            updateDefect(temp).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.getList();
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateDefect(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addDefect(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除缺陷管理编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delDefect(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "system/defect/export",
                {
                    ...this.queryParams,
                },
                `defect_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
